<template>
  <div :class="{'cfCard-index': true, 'cfCard-index-table': hasTable}">
    <div class="cfCard-header" v-if="title">
      <span v-if="title" class="cfCard-header-title">{{title}}</span>
      <span class="cfCard-header-btns" v-if="!hasTable">
        <slot name="btn"></slot>
      </span>
      <span class="cfCard-header-btns" v-else>
        <slot name="btn"></slot>
        <el-dropdown @command="selectTableHeight">
          <span class="el-dropdown-link">
            <svg-icon class="svgIcon" icon-class="height" style="height: 16px;width: 16px;cursor: pointer;" />
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="large">宽松</el-dropdown-item>
            <el-dropdown-item command="default">默认</el-dropdown-item>
            <el-dropdown-item command="small">紧凑</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-popover
          width="240"
          v-model="visible"
          :append-to-body="true"
          placement="left-start"
          trigger="click">
          <div class="cfCard-header-title">配置项</div>
          <el-checkbox-group v-model="tableProps" :min="4" style="max-height: 320px; overflow: overlay;">
            <draggable v-model="tableObj.option">
              <transition-group>
                <div v-for="item in tableObj.option" :key="item[id]" class="checkbox-list">
                  <el-checkbox :label="item.prop">{{item.label}}</el-checkbox>
                </div>
              </transition-group>
            </draggable>
          </el-checkbox-group>
          <div class="popover-footer">
            <el-button size="small" @click="handleCancel">取消</el-button>
            <el-button type="primary" size="small" @click="handleConfirm">确定</el-button>
          </div>
          <svg-icon slot="reference" class="svgIcon" icon-class="configure" style="height: 16px;width: 16px;cursor: pointer;" />
        </el-popover>
      </span>
    </div>
    <div class="cfCard-container">
      <slot name="content"></slot>
    </div>
  </div>
</template>

<script>
import draggable from 'vuedraggable'
  export default {
    props: {
      hasTable: {
        type: Boolean || String,
        default: false
      },
      title: {
        type: String,
        default: ""
      },
      tableObj: {
        type: Object,
        default: (()=>{})
      },
      id: {
        type: String,
        default: 'id'
      }
    },
    components: {draggable},
    inject: ['setTableHeight'],
    data() {
      return {
        tableProps: ['deptName', 'status'],
        visible: false,
      }
    },
    created() {
      if(this.hasTable) {
        this.tableProps = this.tableObj.option.map(item=>item.show && item.prop);
      }
    },
    methods: {
      selectTableHeight(command) {
        let globalHeight = '48px';
        if(command == 'large') {
          globalHeight = '56px';
        } else if(command == 'small') {
          globalHeight = '40px';
        }
        this.setTableHeight(globalHeight);
      },
      handleCancel() {
        this.visible = false;
        this.tableProps = this.tableObj.option.map(item=>item.show && item.prop);
      },
      async handleConfirm() {
        await this.tableObj.option.map(item=>{
          if(this.tableProps.includes(item.prop)) {
            item.show = 1;
          } else {
            item.show = 0;
          }
        })
        await (this.visible = false);
      },
    },
  }
</script>

<style lang="scss">
.cfCard-index {
  width: 100%;
  padding: 16px 16px;
  background: #fff;
  border-radius: 4px;
  &.page-container-search {
    padding-bottom: 0;
    .el-input, .el-select {
      width: 220px;
    }
  }
  .cfCard-header {
    padding: 0 0 8px;
    width: 100%;
    display: flex;
    flex-wrap: nowrap;
    justify-content: space-between;
    align-items: center;
    
    .cfCard-header-title {
      min-width: 80px;
      line-height: 32px;
      flex-shrink: 0;
      font-weight: 600;
      font-size: 16px;
      &::before {
        content: "";
        display: inline-block;
        width: 3px;
        height: 16px;
        background: var(--current-color);
        margin-right: 8px;
        vertical-align: middle;
        margin-top: -2px;
      }
    }
    .cfCard-header-btns {
      width: 100%;
      text-align: right;
      font-size: 14px;
      .svgIcon {
        &:hover {
          color: var(--current-color);
        }
      }
    }
    .cfCard-container {
      
    }
  }
  &.cfCard-index-table {
    padding-top: 0;
    .cfCard-header {
      padding-top: 16px;
      position: sticky;
      top: 0;
      background: #fff;
      z-index: 10;
    }
  }
  .el-dropdown-link {
    display: inline-block;
    width: 24px;
    height: 24px;
    text-align: center;
    line-height: 24px;
    margin-left: 8px;
    margin-right: 8px;
    &:hover {
      background: #E8F4FF;
    }
  }
}
</style>
<style lang="scss">
  .checkbox-list {
    line-height: 24px;
    border-bottom-width: 1px;
    border-bottom-style: dashed;
    border-bottom-color: #DCDCDC;
    cursor: grabbing;
    &::before {
      display: inline-block;
      content: "::";
      margin-right: 24px;
      width: 14px;
      height: 20px;
      color: #656566;
      font-size: 20px;
    }
  }
  .popover-footer {
    margin-top: 8px;
    padding-top: 8px;
    padding-right: 12px;
    border-top: 1px solid #DCDCDC;
    overflow: hidden;
    text-align: right;
    margin-left: -12px;
    margin-right: -12px;
  }
</style>